// pages/detail.js
let qcloud = require('../../utils/wafer2-client-sdk/index.js')
import config from '../../config.js'
import utils from '../../utils/utils.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        todo: {}
    },
    index: NaN,
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.todos = wx.getStorageSync('todo_list')
        this.index = options.index
        let todo = this.todos[options.index]
        todo.done = ''
        todo.not_done = ''
        if (todo.logs && todo.logs.length) {
            this.logs_index = todo.logs.length
        } else {
            todo.logs = []
            this.logs_index = 0
        }
        this.setData({
            todo
        })
        console.log(todo)
    },

    save: function () {
        let todo = this.data.todo
        if (todo.done.trim() || todo.not_done.trim()) {
            todo.logs[this.logs_index] = {
                done: todo.done,
                not_done: todo.not_done,
                progress: todo.progress ? todo.progress : 0,
                time: this.update_time
            }
        } else {
            delete todo.logs[this.logs_index]
        }
        todo.update_time = this.update_time
        this.curTodo = todo
        this.todos[this.index] = todo
        console.log(todo)
        wx.setStorageSync('todo_list', this.todos)
        // 延迟同步
        this.delayHandler = this.delayRequest(this.saveTodoWeb, 2000, this.delayHandler)
    },

    saveTodoWeb() {
        let todo = this.curTodo
        qcloud.request({
            url: config.baseUrl + '/weapp/todo',
            method: 'PUT',
            data: {
                todo: Object.assign(todo, {log: todo.logs[this.logs_index]})
            },
            success: function (response) {
                console.log(response)
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },


    delayRequest(fn, delay, delayHandler) {
        if (delayHandler) {
            clearTimeout(delayHandler)
        }
        return setTimeout(fn, delay)
    },


    goLogs() {
        wx.navigateTo({
            url: '/pages/detail/logs/logs?logs=' + JSON.stringify(this.data.todo.logs)
        })
    },

    inputHandle(e) {
        let field = e.currentTarget.dataset.field
        let todo = this.data.todo
        if (e.detail.value.trim && e.detail.value.trim() === todo[field]) return
        todo[field] = e.detail.value
        this.save()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 本次修改都使用该update_time，
        // 由于数据是实时保存到后台数据库，所以update_time也用于后台判定是否为同一次修改
        this.update_time = utils.formatDate(new Date())
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})